package vn.sms.vsms;
 
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import vn.sms.vsms.data.ServiceItem;
import vn.sms.vsms.provider.DbApdater;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
 
public class MainActivity extends BaseActivity {
	int height;
	int width;
	GridView grid;
	TopMenuAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		ArrayList<ServiceItem> services = DbApdater.getTopMenu();
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		height = displaymetrics.heightPixels;
		width = displaymetrics.widthPixels;		
		grid = (GridView) findViewById(R.id.menu_grid);
		// grid.setExpanded(true);
		TopMenuAdapter adapter = new TopMenuAdapter(this, services,height,width);
		grid.setAdapter(adapter);
		if(Contants.isNetworkAvailable(this))
		{
			//có mạng - check update
			new checkupdateTask().execute();
		}
		
		 
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		return super.onOptionsItemSelected(item);
	}
	
	
	private class checkupdateTask extends AsyncTask<String,Void, String>
	{
		
		ProgressDialog barProgressDialog;
 
		public checkupdateTask() {
			super();
			barProgressDialog = new ProgressDialog(MainActivity.this);						 
			barProgressDialog.setTitle("Thông báo");			
			barProgressDialog.setMessage("Đang cập nhật ...");			
			barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);			
			barProgressDialog.setProgress(0);			
			barProgressDialog.setMax(100);			
		}
		
		
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			
		}



		@Override
		protected String doInBackground(String... params) {			 
			int currentVersion = Contants.getVersion();
			Log.i("logging","update:" + Contants.UPDATE_URL + "version/" + currentVersion);
			final String result = Contants.downLoadUrlInBuffer(Contants.UPDATE_URL + "version/" + currentVersion,
					Contants.HTTP_TIMEOUT);
            Log.i("logging","result:"+result);
            
			return result;
		}
		
		@Override 
		public void onPostExecute(String result) {
			if(result != null &&  !result.isEmpty())
            {
            	try {
					JSONObject json = new JSONObject(result);
					if(json.has("update"))
					{
						int isUpdate = json.getInt("update");
						if(isUpdate == 1)
						{
							barProgressDialog.show();
							
							if(json.has("sql"))
							{
								String sql = json.getString("sql");
								 
								if(sql != null && !sql.isEmpty())
								{
									
									String[] strSQL = sql.split("\r\n");
									 
									for(int i = 0; i < strSQL.length; i ++)
									{
										barProgressDialog.setProgress( (i+1)*100/strSQL.length );
										DbApdater.executeSQL(strSQL[i]);
									}
									
									barProgressDialog.setProgress(100);
					            	barProgressDialog.dismiss();
					            	
					            	if(json.has("version"))
									{
										int currentVersion = json.getInt("version");
										Contants.updateVersion(currentVersion);
									}
									
									ArrayList<ServiceItem> services = DbApdater.getTopMenu();
									TopMenuAdapter adapter = new TopMenuAdapter(MainActivity.this, services,height,width);
									grid.setAdapter(adapter);
								}
							}
						}
						 
					}
					 
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					 
				}
            	
            } 
		
		}
		
		 
		
	}
	
	
	
	

}
